package com.example.coffeeseller_2.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.example.coffeeseller_2.R;
import com.example.coffeeseller_2.model.beans.Taste;
import com.example.coffeeseller_2.utils.DensityUtil;

import java.util.List;

import static android.R.attr.width;

/**
 * Created by WH on 2018/7/30.
 * SeekBar 上的圆圈
 */

public class CiclesView extends View {

    private int cicleNum = 2;
    private Context context;
    private AttributeSet attrs;
//    private int width, height;
    private int cicleRadius = 20;
    private int offsetWidth;
    private int color, textColor;
    private int textSize;
    private int avgLength = 200;
    private int whiteCicleIndex = 0;
    private Paint paint, textPaint, enoughPaint, linePaint;
    private List<Taste> tastes;
    private int mode = 0;
    private int canDragMaxIndex;
    private String notEnough;

    /*
    * 两个圆点间的段数
    * */
    private int lineNum = 6;
    private int lineSpace = 5;//两个线段之间的间隔


    public CiclesView(Context context) {
        super(context);
    }

    public CiclesView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.attrs = attrs;
        init();
    }

    public CiclesView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void init() {
        TypedArray attrsArray = context.obtainStyledAttributes(attrs, R.styleable.CiclesView);
       try {
           cicleRadius = DensityUtil.dp2px(context, (int) attrsArray.getDimension(R.styleable.CiclesView_viewRadius, 10));
           offsetWidth = DensityUtil.dp2px(context, (int) attrsArray.getDimension(R.styleable.CiclesView_offsetWidth, cicleRadius));
           color = attrsArray.getColor(R.styleable.CiclesView_cicleColor, ContextCompat.getColor(context, R.color.colorAccent));
           textColor = attrsArray.getColor(R.styleable.CiclesView_tasteTextColor, ContextCompat.getColor(context, R.color.white));
           textSize = DensityUtil.dp2px(context, (int) attrsArray.getDimension(R.styleable.CiclesView_tasteTextSize, 10));
       }finally {
          attrsArray.recycle();
       }

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(color);

        textPaint = new Paint();
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);

        enoughPaint = new Paint();
        enoughPaint.setColor(textColor);
        enoughPaint.setTextSize(10);

        linePaint = new Paint();
        linePaint.setColor(textColor);
        linePaint.setAntiAlias(true);
        linePaint.setColor(color);
        linePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        linePaint.setStrokeWidth(10);

        notEnough = context.getString(R.string.not_enought);
    }

    /*
    * 设置选中的白色点数
    * */
    public void setCicleNum(int cicleNum) {
        this.cicleNum = cicleNum;
    }

    public int getCicleNum() {
        return cicleNum;
    }

    /*
            * 根据口味的多少，设置节点
            * */
    public void setDatas(List<Taste> tastes) {

        this.tastes = tastes;
        if (tastes != null && tastes.size() > 0) {
            cicleNum = tastes.size() >= 2 ? tastes.size() : 2;
        }

        invalidate();
    }

    /*
    * selectedNum:白色圆点个数 1-N
    * */
    public void changeSelectedStyle(int selectedNum) {

        whiteCicleIndex = selectedNum;// (selectedNum == 0 ? 0 : selectedNum);
        invalidate();
    }

    public int getWhiteCicleIndex() {
        return whiteCicleIndex;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    public void setCanDragMaxIndex(int canDragMaxIndex) {

        this.canDragMaxIndex = canDragMaxIndex;
        invalidate();
    }

    public void setCicleRadius(int cicleRadius) {
        this.cicleRadius = cicleRadius;
        invalidate();

    }

    public void setOffsetWidth(int offsetWidth) {
        this.offsetWidth = offsetWidth;
        invalidate();

    }

    public void setColor(int color) {
        this.color = color;
        invalidate();
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        invalidate();

    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        invalidate();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //setMeasuredDimension(widthMeasureSpec, offsetWidth + textSize + 10);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
       int width = getMeasuredWidth();
       int height = getMeasuredHeight();
        avgLength = (width - offsetWidth) / ((cicleNum - 1) <= 0 ? 1 : (cicleNum - 1));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mode == 0) {
            drawCicleBottom(canvas);

        } else {
            drawCicleTop(canvas);
        }

    }

    private void drawCicleBottom(Canvas canvas) {
        for (int i = 0; i < cicleNum; i++) {

            int startX = offsetWidth / 2 + i * avgLength;

            if (i <= canDragMaxIndex) {
                if (i <= whiteCicleIndex) {
                    paint.setColor(ContextCompat.getColor(context, R.color.white));
                } else {
                    paint.setColor(color);
                }
            } else {

                if (canDragMaxIndex > 0) {
                    paint.setColor(color);

                } else {
                    paint.setColor(ContextCompat.getColor(context, R.color.taste_not_drag));
                }
            }

            paint.setColor(color);

            canvas.drawCircle(startX, getHeight() / 2, cicleRadius, paint);

            //绘制虚线
            if (i>=canDragMaxIndex){
                float length = (avgLength - cicleRadius * 2 - (lineNum - 1) * lineSpace) / lineNum;
                if (i + 1 < cicleNum) {
                    int initX = startX + cicleRadius;
                    for (int index = 0; index < lineNum; index++) {

                        int sX = (int) (initX + index * length + index * lineSpace);
                        int eX = (int) (sX + length);
                        canvas.drawLine(sX, getHeight() / 2, eX, getHeight() / 2, linePaint);
                    }
                }

            }

            //绘制文本
            if (tastes != null && tastes.size() > 0) {
                if (i >= tastes.size()) {
                    continue;
                }
                String text = tastes.get(i).getRemark();

                if (TextUtils.isEmpty(text)) {
                    continue;
                }

                if (i > canDragMaxIndex) {
                    Rect rect = new Rect();
                    textPaint.getTextBounds(text, 0, text.length(), rect);
                    Rect notEnoughRect = new Rect();
                    enoughPaint.getTextBounds(notEnough, 0, notEnough.length(), notEnoughRect);

                    canvas.drawText(text, startX - rect.width() / 2 - notEnoughRect.width() / 2, getHeight() / 2 + offsetWidth / 2 + 10, textPaint);

                    canvas.drawText(notEnough, startX - rect.width() / 2 - notEnoughRect.width() / 2 + rect.width(), getHeight() / 2 + offsetWidth / 2 + 10, enoughPaint);
                } else {
                    Rect rect = new Rect();
                    textPaint.getTextBounds(text, 0, text.length(), rect);
                    canvas.drawText(text, startX - rect.width() / 2, getHeight() / 2 + offsetWidth / 2 + 10, textPaint);
                }
            }
        }
    }

    private void drawCicleTop(Canvas canvas) {
        for (int i = 0; i <= whiteCicleIndex; i++) {

            if (i == canDragMaxIndex) {
                break;
            }
            int startX = offsetWidth / 2 + i * avgLength;
            paint.setColor(ContextCompat.getColor(context, R.color.white));
            canvas.drawCircle(startX, getHeight() / 2, cicleRadius, paint);
        }
    }
}
